//
//  GoodViewController.m
//  庐山通
//
//  Created by zcj on 15-10-23.
//  Copyright (c) 2015年 郑朝军. All rights reserved.
//

#import "GoodViewController.h"
#import "MJExtension.h"
#import "MJRefresh.h"
#import "GoodTableViewCell.h"
#import "GoodDataFrame.h"
#import "SelectFindHttp.h"
#import "Grouble.h"
#import "GoodData.h"
#import "MBProgressHUD+MJ.h"
#import "SlideTopViewTwo.h"


@interface GoodViewController ()<MJRefreshBaseViewDelegate,UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) MJRefreshHeaderView *header;
@property (nonatomic,strong) MJRefreshFooterView *footer;
@property (nonatomic,weak)UITableView * MytableView;
@property (nonatomic,strong)NSMutableArray * JindataFrame;
@property (nonatomic,strong)NSMutableArray * advImageUrlArray;

@end

@implementation GoodViewController

- (NSMutableArray *)advImageUrlArray
{
    if (_advImageUrlArray == nil) {
        _advImageUrlArray = [[NSMutableArray alloc]init];
        
    }
    return _advImageUrlArray;
}
- (NSMutableArray *)JindataFrame
{
    if (_JindataFrame == nil) {
        _JindataFrame = [[NSMutableArray alloc]init];
    }
    return _JindataFrame;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self addTableView];
    [self setRush];
    [self addHeader];
    self.view.backgroundColor = [UIColor grayColor];
    // Do any additional setup after loading the view.
}
- (void)addHeader
{
    CGFloat myTopViewH = 250;
    CGFloat GoodViewW = self.view.width;
    SlideTopViewTwo * slideTwo = [[SlideTopViewTwo alloc]initWithFrame:CGRectMake(0, 0, GoodViewW, myTopViewH)];
    [self loadHeaderSlide];
    for (int i = 0; i < self.advImageUrlArray.count; i ++) {
        slideTwo.urlStr = self.advImageUrlArray[i];
    }
    
    self.MytableView.tableHeaderView = slideTwo;
}

- (void)addTableView
{
    UITableView * myTableView = [[UITableView alloc]init];
    myTableView.frame = self.view.bounds;
    myTableView.delegate = self;
    myTableView.dataSource = self;
    [self.view addSubview:myTableView];

    self.MytableView = myTableView;
    
}
- (void)setRush
{
    MJRefreshHeaderView * head = [[MJRefreshHeaderView alloc]init];
    head.scrollView = self.MytableView;
    head.delegate = self;
    [head beginRefreshing];
    self.header = head;
    MJRefreshFooterView * foot = [[MJRefreshFooterView alloc]init];
    foot.scrollView = self.MytableView;
    foot.delegate = self;
    self.footer = foot;
}
- (void)refreshViewBeginRefreshing:(MJRefreshBaseView *)refreshView
{
    if ([refreshView isKindOfClass:[MJRefreshFooterView class]]) { // 上拉刷新
        [self loadMoreData];
    } else { // 下拉刷新
        [self loadNewData];
    }
}
- (void)loadMoreData
{
    
}
// 加载头部滑动
- (void)loadHeaderSlide
{
    if (self.advImageUrlArray.count == 0) {
        [self.advImageUrlArray addObject:@"http://img60.ddimg.cn/digital/product/19/1/1960071901_ii_cover.jpg?version=null"];
        [self.advImageUrlArray addObject:@"http://img60.ddimg.cn/digital/product/19/1/1960071901_ii_cover.jpg?version=null"];
    }
}
- (void)loadNewData
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
       // [self loadHeaderSlide];
    });
    [SelectFindHttp getWirhUrl:HGURL parms:nil success:^(id json) {
        NSMutableArray *bannerFrame=[NSMutableArray array];
        
        NSArray *tempGoods = json[@"data"][@"goods_list"];
        NSLog(@"%@",tempGoods);
        
        // 获取模型对象
        NSArray *findArrayTemp = [GoodData objectArrayWithKeyValuesArray:tempGoods];
        
        for(GoodData * goodData in findArrayTemp){
            GoodDataFrame * goodDataFrame = [[GoodDataFrame alloc]init];
            goodDataFrame.goodData = goodData;
            [bannerFrame addObject:goodDataFrame];
        }
        self.JindataFrame = bannerFrame;
        [self.MytableView reloadData];
        [self.header endRefreshing];
        [MBProgressHUD showSuccess:@"上拉刷新成功"];
    } failture:^(id error) {
        NSLog(@"%@",error);
    }];

    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.JindataFrame.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    GoodTableViewCell * cell = [GoodTableViewCell GoodTableViewCell:tableView];
    GoodDataFrame *goodDataFrame = self.JindataFrame[indexPath.row];
    cell.goodDataFrame = goodDataFrame;
    
    return cell;
    
}
// 返回每一行的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    GoodDataFrame * goodDataFrame = self.JindataFrame[indexPath.row];
   
    return goodDataFrame.cellH;
}

- (void)dealloc
{
    [self.footer free];
    [self.header free];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
